package com.b511.swpu.cementbackend.controller;

import com.b511.swpu.cementbackend.service.DevService;
import com.b511.swpu.cementbackend.service.MetaDictService;
import com.b511.swpu.cementbackend.dto.input.MetaDictInputDto;
import com.b511.swpu.cementbackend.entity.*;
import com.b511.swpu.cementbackend.repository.*;
import com.b511.swpu.cementbackend.service.SysMenuService;
import com.b511.swpu.cementbackend.util.enumeration.Code;
import com.b511.swpu.cementbackend.util.CodeUtil;
import com.b511.swpu.cementbackend.util.R;
import io.swagger.annotations.Api;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;
import java.util.stream.Collectors;

@Api(value = "开发者使用", tags = {"开发者使用"})
@RestController
@RequestMapping("/dev")
public class DevController {

    private final MetaTableRepository metaTableRepository;

    private final MetaFieldRepository metaFieldRepository;

    private final MetaUnitCategoryRepository metaUnitCategoryRepository;

    private final DevService devService;

    private final MetaDictService metaDictService;

    private final MetaDictRepository metaDictRepository;

    private final MetaUnitCategoryValueRepository metaUnitCategoryValueRepository;

    private final SysMenuService sysMenuService;

    public DevController(MetaTableRepository metaTableRepository, MetaFieldRepository metaFieldRepository, MetaUnitCategoryRepository metaUnitCategoryRepository, DevService devService, CodeUtil codeUtil, MetaDictService metaDictService, MetaDictRepository metaDictRepository, MetaUnitCategoryValueRepository metaUnitCategoryValueRepository,
                         SysMenuService sysMenuService) {
        this.metaTableRepository = metaTableRepository;
        this.metaFieldRepository = metaFieldRepository;
        this.metaUnitCategoryRepository = metaUnitCategoryRepository;
        this.devService = devService;
        this.metaDictService = metaDictService;
        this.metaDictRepository = metaDictRepository;
        this.metaUnitCategoryValueRepository = metaUnitCategoryValueRepository;
        this.sysMenuService = sysMenuService;
    }

    @GetMapping("/table")
    public R getAllMetaTable() {
        return R.ok().putData(metaTableRepository.findAll());
    }

    @GetMapping("/table/{id}")
    public R getOneMetaTableFields(@PathVariable Long id) {
        Optional<MetaTable> sysMetaTable = metaTableRepository.findById(id);
        if (!sysMetaTable.isPresent()) {
            return R.error(Code.SELECT_FAIL);
        }
        return R.ok().putData(sysMetaTable.get().getMetaFields());
    }

    @GetMapping("/unit_category")
    public R getAllMetaUnitCategory() {
        return R.ok().putData(metaUnitCategoryRepository.findAll());
    }

    @GetMapping("/dict/level1")
    @Transactional
    public R getDictLevel1() {
        return R.ok().putData(metaDictRepository.findParentDict(MetaDict.DICT_LEVEL_LENGTH).collect(Collectors.toList()));
    }

    @PutMapping("/table")
    @Transactional
    public R updateMetaTable(@RequestBody MetaTable metaTable) {
        metaTableRepository.save(metaTable);
        return R.ok();
    }

    @PutMapping("/field")
    @Transactional
    public R updateMetaField(@RequestBody MetaField metaField) {
        metaFieldRepository.save(metaField);
        return R.ok();
    }

    @PostMapping("/init")
    public R initTableAndField() {
        devService.initTableAndField();
        return R.ok();
    }

    @PostMapping("/dict")
    @Transactional
    public R addDict(@RequestBody @Validated MetaDictInputDto metaDictInputDto) {
        return metaDictService.addDict(metaDictInputDto);
    }

//    @PostMapping("/category")
//    @Transactional
//    public R addCategory(@RequestBody MetaUnitCategory metaUnitCategory) {
//        metaUnitCategoryRepository.save(metaUnitCategory);
//        metaUnitCategoryValueRepository.save(new MetaUnitCategoryValue()
//                .setUnitName(metaUnitCategory.getInternationalUnits())
//                .setMetaUnitCategory(metaUnitCategory));
//        return R.ok();
//    }

    @PostMapping("/dict/{code}")
    public R addDictChild(@PathVariable String code, @RequestBody @Validated MetaDictInputDto metaDictInputDto) {
        return metaDictService.addDictChild(code, metaDictInputDto);
    }
//
//    @PostMapping("/category/{id}")
//    @Transactional
//    public R addCategoryValue(@RequestBody MetaUnitCategoryValue metaUnitCategoryValue, @PathVariable long id) {
//        metaUnitCategoryValue.setMetaUnitCategory(new MetaUnitCategory().setId(id));
//        metaUnitCategoryValueRepository.save(metaUnitCategoryValue);
//        return R.ok();
//    }

    @PostMapping("/menu")
    @Transactional
    public R addMenu(@RequestBody @Validated SysMenu sysMenu){
        return sysMenuService.addMenu(sysMenu);
    }
}
